package com.sixth.controller.wx;

import com.sixth.bean.MarketBrand;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.MarketBrandListVO;
import com.sixth.constant.Constant;
import com.sixth.service.AdminBrandService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @since 2022/09/11 10:19
 * @author zhaoxiangyang
 */

@RestController
@RequestMapping("wx/brand")
public class WXBrandController {

    @Autowired
    // 借用AdminBrandService;没有新建WXBrandService
    AdminBrandService adminBrandService;

    @RequestMapping("detail")
    public BaseRespVo wxBrandDetail(Integer id){
        MarketBrand marketBrand = adminBrandService.wxBrandDetail(id);
        return BaseRespVo.success(marketBrand);
    }

    // 品牌列表
    @RequestMapping("list")
    public BaseRespVo wxBrandListBug(Integer page, Integer limit){
        //小程序请求没有排序参数，参考后台及前台，设置根据增加时间，降序
        // String mySort = "add_time";String myOrder = "desc";
        BasePageInfo basePageInfo = new BasePageInfo(page,limit, Constant.SORT_ADD_TIME,Constant.ORDER_DESC);
        Object brandListVOS = adminBrandService.wxBrandList(basePageInfo);
        BasePageInfoVo brandListVOS1 = (BasePageInfoVo) brandListVOS;//Object无法getlist,强转后可以getlist
        List listVOS1List = brandListVOS1.getList();
        ArrayList<MarketBrandListVO> marketBrandListVOS = new ArrayList<>();
        for (Object o : listVOS1List) {
            MarketBrandListVO marketBrandListVO = new MarketBrandListVO();
            BeanUtils.copyProperties(o,marketBrandListVO);//data里面的list转换成listvo
            marketBrandListVOS.add(marketBrandListVO);
        }
        brandListVOS1.setList(marketBrandListVOS);//把listvo赋值给data
        return BaseRespVo.ok(brandListVOS1);
    }
    // 品牌列表
    @RequestMapping("listLarge")//！！！！如果修改成 "list",同时注释上面的list；可以正常实现wx/brand/list
    public BaseRespVo wxBrandList(Integer page, Integer limit){
        BasePageInfo basePageInfo = new BasePageInfo(page,limit,Constant.SORT_ADD_TIME,Constant.ORDER_DESC);
        // List<MarketBrandListVO> brandListVOS = adminBrandService.wxBrandListLarge(basePageInfo);
        BasePageInfoVo<MarketBrandListVO> brandListVOBasePageInfoVo = adminBrandService.wxBrandListLarge(basePageInfo);
        return BaseRespVo.ok(brandListVOBasePageInfoVo);
    }
}
